package org.dromara.xxmj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.xxmj.domain.UserStudyRecord;
import org.dromara.xxmj.domain.dto.StudyProgressDto;
import org.dromara.xxmj.domain.vo.CourseVideoDetailVo;
import org.dromara.xxmj.domain.vo.CourseVideoVo;
import org.dromara.xxmj.mapper.CourseVideoMapper;
import org.dromara.xxmj.mapper.UserStudyRecordMapper;
import org.dromara.xxmj.service.ICourseService;
import org.dromara.xxmj.service.ICourseVideoService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 课程视频服务实现类
 *
 * @author Vincent
 */
@RequiredArgsConstructor
@Service
public class CourseVideoServiceImpl implements ICourseVideoService {

    private final CourseVideoMapper videoMapper;
    private final UserStudyRecordMapper studyRecordMapper;
    private final ICourseService courseService;

    /**
     * 获取章节下的视频列表
     *
     * @param chapterId 章节ID
     * @return 视频列表
     */
    @Override
    public List<CourseVideoVo> listVideosByChapter(Long chapterId) {
        return videoMapper.selectVideosByChapterId(chapterId);
    }

    /**
     * 获取视频详情
     *
     * @param id 视频ID
     * @return 视频详情
     */
    @Override
    public CourseVideoDetailVo getVideoDetail(Long id) {
        Long userId = LoginHelper.getUserId();
        CourseVideoDetailVo vo = videoMapper.selectVideoDetailWithUserProgress(id, userId);
        
        // 更新观看次数
        videoMapper.incrementViewCount(id);
        
        return vo;
    }

    /**
     * 更新视频观看进度
     *
     * @param dto 进度信息
     * @return 是否成功
     */
    @Override
    public Boolean updateProgress(StudyProgressDto dto) {
        Long userId = LoginHelper.getUserId();
        Long videoId = dto.getVideoId();
        
        // 检查是否存在记录
        LambdaQueryWrapper<UserStudyRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserStudyRecord::getUserId, userId)
                   .eq(UserStudyRecord::getVideoId, videoId);
        
        UserStudyRecord record = studyRecordMapper.selectOne(queryWrapper);
        
        if (record != null) {
            // 更新进度
            record.setProgress(dto.getProgress());
            if (StringUtils.isNotEmpty(dto.getStatus())) {
                record.setStatus(dto.getStatus());
            }
            return studyRecordMapper.updateById(record) > 0;
        } else {
            // 新增记录
            record = new UserStudyRecord();
            record.setUserId(userId);
            record.setVideoId(videoId);
            record.setProgress(dto.getProgress());
            record.setStatus(StringUtils.isNotEmpty(dto.getStatus()) ? dto.getStatus() : "0");
            return studyRecordMapper.insert(record) > 0;
        }
    }

    /**
     * 获取用户视频观看进度
     *
     * @param videoId 视频ID
     * @return 观看进度（秒）
     */
    @Override
    public Integer getVideoProgress(Long videoId) {
        Long userId = LoginHelper.getUserId();
        Integer progress = studyRecordMapper.getVideoProgress(userId, videoId);
        return progress != null ? progress : 0;
    }
} 